import { cloneDeep } from 'lodash'
const tagsView = {
  namespaced: true,
  state: {
    visitedViews: [],
    cachedViews: [],
    isPageLoadFirst: true
  },
  mutations: {
    ADD_VISITED_VIEW: (state, payload) => {
      state.isPageLoadFirst = false
      state.visitedViews.push(payload)
    },
    DELETE_SOME_VISTIED_VIEW: (state, index) => {
      state.visitedViews.splice(index, 1)
    },
    DELETE_ALL_VISTIED_VIEW: (state, index) => {
      state.visitedViews = []
    },
    DELETE_OTHER_VISTIED_VIEW: (state, index) => {
      var tag = state.visitedViews[index]
      state.visitedViews = tag ? [tag] : []
    },
    ADD_CACHED_VIEW: (state, payload) => {
      state.cachedViews.push(payload)
    },
    DELETE_ALL_CACHED_VIEW: (state) => {
      state.cachedViews = []
    },
    DELETE_SOME_CACHED_VIEW: (state, index) => {
      state.cachedViews.splice(index, 1)
    },
    DELETE_OTHER_CACHED_VIEW: (state, index) => {
      var tag = state.cachedViews[index]
      state.cachedViews = tag ? [tag] : []
    }
  },
  actions: {
    addVisitedTagsView({ commit, state, dispatch }, tag = {}) {
      var {
        resourceName,
        routeName,
        resourceCode,
        query,
        fullPath,
        path
      } = tag
      dispatch('addCacheView', tag)
      if (state.visitedViews.some(item => path === item.path)) return
      commit('ADD_VISITED_VIEW', {
        resourceName,
        routeName,
        resourceCode,
        query,
        fullPath,
        path
      })
    },
    delSomeTagsView({ commit, state }, tag) {
      var { path } = tag
      var index = -1
      for (let i = 0, len = state.visitedViews.length; i < len; i++) {
        if (state.visitedViews[i].path === path) {
          index = i
          break
        }
      }
      commit('DELETE_SOME_VISTIED_VIEW', index)
      commit('DELETE_SOME_CACHED_VIEW', index)
      return cloneDeep({ visitedViews: state.visitedViews })
    },
    delOthersTagsViews({ commit, state }, tag) {
      var { path } = tag
      var index = -1
      for (let i = 0, len = state.visitedViews.length; i < len; i++) {
        if (state.visitedViews[i].path === path) {
          index = i
          break
        }
      }
      commit('DELETE_OTHER_VISTIED_VIEW', index)
      commit('DELETE_OTHER_CACHED_VIEW', index)
      return cloneDeep({ visitedViews: state.visitedViews })
    },
    delAllTagsView({ commit, state }, payload = {}) {
      commit('DELETE_ALL_VISTIED_VIEW')
      commit('DELETE_ALL_CACHED_VIEW')
      return cloneDeep({ visitedViews: state.visitedViews })
    },
    delSomeCacheView({ commit, state }, tag) {
      var { path } = tag
      var index = -1
      for (let i = 0, len = state.visitedViews.length; i < len; i++) {
        if (state.visitedViews[i].path === path) {
          index = i
          break
        }
      }
      commit('DELETE_SOME_CACHED_VIEW', index)
      return cloneDeep({ cachedViews: state.cachedViews, visitedViews: state.visitedViews })
    },
    addCacheView({ commit, state }, tag = {}) {
      var { routeName } = tag
      if (state.cachedViews.some(name => name === routeName)) return
      commit('ADD_CACHED_VIEW', routeName)
    }
  }
}

export default tagsView
